package com.baohulu.baohulu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;

import com.baohulu.baohulu.R;
import com.baohulu.baohulu.common.Const;
import com.baohulu.baohulu.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.core.QRCodeView;

/**
 * 二维码扫描界面
 */
public class ScanActivity extends AppCompatActivity implements QRCodeView.Delegate {

    private static final String TAG = "ScanActivity";
    @BindView(R.id.scan_view)
    QRCodeView qrView;
    @BindView(R.id.open_light)
    Button btnOpenLight;
    boolean isLightOpen = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        ButterKnife.bind(this);
        qrView.setDelegate(this);
    }

    @Override
    protected void onStop() {
        qrView.stopCamera();
        super.onStop();

    }

    @Override
    protected void onDestroy() {
        qrView.onDestroy();
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        qrView.stopSpot();
    }

    @Override
    protected void onResume() {
        super.onResume();
        qrView.startCamera();
        qrView.startSpot();
    }

    @OnClick(R.id.open_light)
    public void openLight() {
        if (isLightOpen) {
            qrView.closeFlashlight();
            btnOpenLight.setText(R.string.open_flash);
            isLightOpen = false;
        } else {
            qrView.openFlashlight();
            btnOpenLight.setText(R.string.close_light);
            isLightOpen = true;
        }
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        Log.i(TAG, "QRCode result:" + result);
        vibrate();
        if (TextUtils.isEmpty(result)) {
            //没有扫到
            ToastUtil.showToast("二维码无效！");
            qrView.startSpot();
        } else {
            //对二维码信息进行解析
            String ballKey = getBallKeyFromUrl(result);
            String eqKey = getEqKeyFromUrl(result);
            int doorId = getDoorIndexFromUrl(result);
            if (TextUtils.isEmpty(ballKey) || TextUtils.isEmpty(eqKey) || doorId == -1) {
                ToastUtil.showToast("二维码无效！");
                qrView.startSpot();
            } else {
                goToRentInfoActivity(ballKey, eqKey, doorId);
            }
        }

    }

    private void goToRentInfoActivity(String ballKey, String eqKey, int doorId) {
        //跳转到开锁页面
        Intent intent = new Intent(this, RentInfoActivity.class);
        intent.putExtra(Const.Extra.BALL_KEY, ballKey);
        intent.putExtra(Const.Extra.EQ_KEY, eqKey);
        intent.putExtra(Const.Extra.DOOR_INDEX, doorId);
        startActivity(intent);
        finish();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        if (null != vibrator) {
            vibrator.vibrate(200);
        }
    }

    /**
     * 从url中提取球的标识
     *
     * @param qrInfo 二维码中的url
     * @return 球的key
     */
    public String getBallKeyFromUrl(String qrInfo) {
        String ballKeyStr = "";
        if (!qrInfo.contains("ballkey")) {
            return ballKeyStr;
        }
        try {
            ballKeyStr = qrInfo.substring(qrInfo.indexOf("ballkey") + 8, qrInfo.indexOf('&'));
        } catch (Exception e) {
            Log.d(TAG, "getBallKeyFromUrl: 获取ballKey出错");
        }
        return ballKeyStr;
    }

    public String getEqKeyFromUrl(String qrInfo) {
        String eqKeyStr = "";
        if (!qrInfo.contains("eqkey")) {
            return eqKeyStr;
        }
        try {
            eqKeyStr = qrInfo.substring(qrInfo.indexOf("eqkey") + 6, qrInfo.lastIndexOf('&'));
        } catch (Exception e) {
            Log.d(TAG, "getEqKeyFromUrl: 获取eqKey出错");
        }
        return eqKeyStr;
    }

    public int getDoorIndexFromUrl(String qrInfo) {
        int doorIndex = -1;
        if (!qrInfo.contains("doorid")) {
            return doorIndex;
        }
        try {
            String doorIndexStr = qrInfo.substring(qrInfo.lastIndexOf('=') + 1);
            doorIndex = Integer.valueOf(doorIndexStr);
        } catch (Exception e) {
            Log.d(TAG, "getEqKeyFromUrl: 获取doorid出错");
        }
        return doorIndex;
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        ToastUtil.showToast("打开相机出错");
        finish();
    }

}
